package com.zs.toolbox.web2j.system.core.controller.auth;


import com.zs.toolbox.web2j.common.exception.WtResponses;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtAuthContext;
import com.zs.toolbox.web2j.common.web.WtBaseController;
import com.zs.toolbox.web2j.system.core.request.WtOpenAuthLoginRequest;
import com.zs.toolbox.web2j.system.core.request.WtWxBindOrUnbindRequest;
import com.zs.toolbox.web2j.system.core.response.WtOpenUserLoginResponse;
import com.zs.toolbox.web2j.system.core.service.WtUserOpenService;
import com.zs.toolbox.web2j.system.core.service.WtUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户基本信息操作
 * </p>
 *
 * @author eric.zhang
 * @since 2018-09-21
 */
@RestController
@RequestMapping("/api/v1/user/open/")
@Api("第三方认证安全管理")
@Slf4j
public class WtOpenAuthController extends WtBaseController {

    @Autowired(required = false)
    private WxMpService wxService;

    @Autowired
    WtUserOpenService wtUserOpenService;

    @Autowired
    WtUserService wtUserService;

//    @Autowired
//    private WxMpMessageRouter router;

    /*开放账号绑定*/
    @RequestMapping(value = "/bind", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("开放账号解绑")
    @PreAuthorize(("isAuthenticated()"))
    public WtBaseResponse wxBind(WtWxBindOrUnbindRequest request) {
        this.volidate(request);
        wtUserOpenService.bind(request.getOpenType(), request.getOpenId(), WtAuthContext.getUserId());
        return WtResponses.SUCCESS;
    }

    /*开放账号解绑*/
    @ApiOperation("开放账号绑定")
    @PreAuthorize(("isAuthenticated()"))
    @RequestMapping(value = "/unbind", method = {RequestMethod.POST, RequestMethod.GET})
    public WtBaseResponse unBind(WtWxBindOrUnbindRequest request) {
        this.volidate(request);
        wtUserOpenService.unbind(request.getOpenType(), request.getOpenId(), WtAuthContext.getUserId());
        return WtResponses.SUCCESS;
    }

    /**
     * 进行微信登陆操作，如果有绑定关系，直接返回TOKEN，如无绑定关系，则创建
     *
     * @param code
     * @param state
     * @return
     */
    /*微信登陆*/
    @ApiOperation("微信登陆操作")
    @RequestMapping(value = "/wx/login", method = {RequestMethod.POST, RequestMethod.GET})
    public WtBaseResponse<WtOpenUserLoginResponse> wxSiteLogin(WtOpenAuthLoginRequest request) {
        this.volidate(request);
        return wtUserOpenService.wxLogin(request.getCode());
    }

    /*QQ登陆*/
    @ApiOperation("QQ登陆操作")
    @RequestMapping(value = "/qq/login", method = {RequestMethod.POST, RequestMethod.GET})
    public WtBaseResponse<WtOpenUserLoginResponse> qqSiteLogin(WtOpenAuthLoginRequest request ) {
        this.volidate(request);
        return wtUserOpenService.qqLogin(request.getCode(),request.getState());
    }


    /*微博登陆*/


    @GetMapping(produces = "text/plain;charset=utf-8")
    @ApiOperation("微信服务器认证识别")
    public String authGet(
            @RequestParam(name = "signature",
                    required = false) String signature,
            @RequestParam(name = "timestamp",
                    required = false) String timestamp,
            @RequestParam(name = "nonce", required = false) String nonce,
            @RequestParam(name = "echostr", required = false) String echostr) {

        this.log.info("\n接收到来自微信服务器的认证消息：[{}, {}, {}, {}]", signature,
                timestamp, nonce, echostr);

        if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
            throw new IllegalArgumentException("请求参数非法，请核实!");
        }

        if (this.wxService.checkSignature(timestamp, nonce, signature)) {
            return echostr;
        }

        return "非法请求";
    }
}